/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.controller;

import com.seari.bean.UserPrivilege;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.MessageBean;
import com.seari.pojo.QueryUserPrivilege;
import com.seari.service.UserPrivilegeService;
import com.seari.utils.StringHelper;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Rainbow
 */
@Controller
@RequestMapping("userPrivilege/")
public class UserPrivilegeController
{
    @Autowired
    private UserPrivilegeService userPrivilegeService;
    
    @RequestMapping("getUserPrivilegeByParams")
    @ResponseBody
    public DataGridBean<UserPrivilege> getUserPrivilegeByParams(HttpServletRequest req)
    {
        DataGridBean<UserPrivilege> grid = new DataGridBean<UserPrivilege>();
        QueryUserPrivilege qup = new QueryUserPrivilege();
        List<UserPrivilege> privileges = userPrivilegeService.getUserPrivilegeByParams(qup);
        grid.setRows(privileges);
        grid.setTotal(privileges.size());
        return grid;
    }
    
    @RequestMapping("addUserPrivilege")
    @ResponseBody
    public MessageBean addUserPrivilege(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String privilege = req.getParameter("privilege");
        String privilegeName = req.getParameter("privilegeName");
        if(StringUtils.isBlank(privilege) || StringUtils.isBlank(privilegeName))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }else{
            QueryUserPrivilege qup = new QueryUserPrivilege();
            qup.setPrivilege(Integer.parseInt(privilege));
            qup.setPrivilegeName(privilegeName);
            int resultNum = userPrivilegeService.checkUserPrivilegeExist(qup);
            if(resultNum > 0 )
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("权限位或权限名重复");
                return mb;
            }
            UserPrivilege userPrivilege = new UserPrivilege();
            userPrivilege.setPrivilege(Integer.parseInt(privilege));
            userPrivilege.setPrivilegeName(privilegeName);
            int resultNum1 = userPrivilegeService.addUserPrivilege(userPrivilege);
            if(resultNum1 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("权限添加失败");
            }else{
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("权限添加成功");
            }
        }
        return mb;
    }
    
    
    @RequestMapping("updateUserPrivilege")
    @ResponseBody
    public MessageBean updateUserPrivilege(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String upid = req.getParameter("upid");
        String privilege = req.getParameter("privilege");
        String privilegeName = req.getParameter("privilegeName");
        if(StringUtils.isBlank(upid) || StringUtils.isBlank(privilege) || StringUtils.isBlank(privilegeName))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }else{
            QueryUserPrivilege qup = new QueryUserPrivilege();
            qup.setUpid(Integer.parseInt(upid));
            qup.setPrivilege(Integer.parseInt(privilege));
            qup.setPrivilegeName(privilegeName);
            int resultNum = userPrivilegeService.checkUserPrivilegeExist(qup);
            if(resultNum > 0 )
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("权限位或权限名重复");
                return mb;
            }
            UserPrivilege userPrivilege = new UserPrivilege();
            userPrivilege.setUpid(Integer.parseInt(upid));
            userPrivilege.setPrivilege(Integer.parseInt(privilege));
            userPrivilege.setPrivilegeName(privilegeName);
            int resultNum1 = userPrivilegeService.updateUserPrivilege(userPrivilege);
            if(resultNum1 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("修改失败");
            }else{
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("修改成功");
            }
        }
        return mb;
    }
    
    @RequestMapping("deleteUserPrivilege")
    @ResponseBody
    public MessageBean deleteUserPrivilege(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String upid = req.getParameter("upid");
        if(StringUtils.isBlank(upid))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }else
        {  
            int resultNum1 = userPrivilegeService.deleteUserPrivilege(Integer.parseInt(upid));
            if(resultNum1 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("删除失败");
            }else{
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("删除成功");
            }
        }
        return mb;
    }
}
